#include "mainscene.h"
#include "ui_mainscene.h"
#include"config.h"
#include<QIcon>
#include<QPainter>
#include<ctime>
#include"success.h"
#include<QMouseEvent>
#include"button.h"
#include"start.h"
#include"startbutton.h"
mainscene::mainscene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::mainscene)
{
    ui->setupUi(this);
    connect(ui->actionquit,&QAction::triggered,[=](){
            this->close();
        });

    initscene();//调用初始化场景
    //启动游戏
        playgame();

}

mainscene::~mainscene()
{
    delete ui;
}


void mainscene::initscene()
{
    setFixedSize(game_width,game_height);//设置窗口固定尺寸
    setWindowTitle(game_title);
    setWindowIcon(QIcon(game_icon));//加载图标
    m_timer.setInterval(game_rate);
    recorder=0;
srand((unsigned int)time(NULL));
}

void mainscene::playgame()
{QSound::play(sound_background);
    //启动定时器

    m_timer.start();
    connect(&m_timer,&QTimer::timeout ,[=](){collision();  friendstoscene();updatePosition();update();  }  );



}

void mainscene::updatePosition()
{m_map.mapposition();
m_duola.shoot();//发射铜锣烧！

for(int i=0;i<BULLET_NUM;i++)
{if(m_duola.m_bullets[i].free==false)
    m_duola.m_bullets[i].updateposition();
}
//朋友
for(int k=0;k<5;k++){
for(int i=0;i<friends_num;i++)
    {if(m_friends[i].free[k]==false)

           { m_friends[i].updatePostion();}
}
}
for(int i=0;i<money_num;i++)
{if(num_money[i].free==false)
    {num_money[i].setmoneyposition();}
}
/*for(int i=0;i<mood_num;i++)
{if(m_mood[i].free==false)
    {m_mood[i].setmoodposition();}
}
*/
}
void mainscene::paintEvent(QPaintEvent *)
{ QPainter painter(this);
    painter.drawPixmap(0,0,m_startbutton.m_start);
    painter.drawPixmap(-1,-1,m_startbutton.mouse);
    painter.drawPixmap(game_width/2-45,game_height*0.83,m_button.button);

   if(m_startbutton.free==false)
   {
    painter.drawPixmap(m_map.map1_x,0,m_map.m_map1);
    painter.drawPixmap(m_map.map2_x,0,m_map.m_map2);
    painter.drawPixmap(m_duola.m_x,m_duola.m_y,m_duola.duola);
    for(int i=0;i<mood_num;i++)
    {
        painter.drawPixmap(20+50*i,15,m_premood[i].pre_mood);
    }
    //子弹（铜锣烧）
    for(int i=0;i<BULLET_NUM;i++)
    {if(m_duola.m_bullets[i].free==false)
      {
            painter.drawPixmap(m_duola.m_bullets[i].x,m_duola.m_bullets[i].y,m_duola.m_bullets[i].m_bullet);
        }
    }
    //朋友
   for(int k=0;k<friends_max;k++){

       for(int i=0;i<friends_num;i++)
        {if(m_friends[i].free[k]==false)

               { painter.drawPixmap(m_friends[i].x[k],m_friends[i].y[k],m_friends[i].m_friend[k]);}

    }
}
   for(int i=0;i<money_num;i++)
   {if(num_money[i].free==false)
       {painter.drawPixmap(num_money[i].x,num_money[i].y,num_money[i].m_money);}
   }
   for(int i=0;i<mood_num;i++)
   {if(m_mood[i].free==false)
       {painter.drawPixmap(m_mood[i].x,m_mood[i].y,m_mood[i].mood);}
   }
   if(m_win.free==false)
   { painter.drawPixmap(m_win.x,m_win.y,m_win.win);}
}
}

void mainscene::mouseMoveEvent(QMouseEvent *event)
{if(m_startbutton.free==true)
    {
         int x=event->x()-m_startbutton.rect.width()/2;
         int y=event->y()-m_startbutton.rect.height()/2;
         //边界的限制

        m_startbutton.followpos(x,y);
}
    else
    { int x=event->x()-m_duola.rect.width()/2;
    int y=event->y()-m_duola.rect.height()/2;
    //边界的限制
    if(x<=0)
    {x=0;}
    if(x>=game_width-m_duola.rect.width())
          {x=game_width-m_duola.rect.width();}
    if(y<=0)
    {y=0;}
    if(y>=game_height-m_duola.rect.height())
          {y=game_height-m_duola.rect.height();}
    m_duola.setposition(x,y);
}}

void mainscene::friendstoscene()
{
    if(m_startbutton.free==false)
    {
    recorder++;
    if(recorder<friends_interval)
    {return;}
recorder=0;
for(int i=0;i<friends_num;i++)
  {  int k=rand()%(4+1);
if(m_friends[i].free[k])
   { m_friends[i].free[k]=false;
   m_friends[i].x[k]=game_width-m_friends[i].rect[k].width();
    m_friends[i].y[k]=rand()%(game_height-m_friends[k].rect[k].height());

   break; }
//game_height/2
}
    }
}



void mainscene::collision()
{if(m_startbutton.rect.intersects(m_button.rect))
    {m_startbutton.free=false;}

    int k=rand()%(4+1);
    for(int i=0;i<friends_num;i++)
    {
        if(m_friends[i].free[k])
    {continue;}

    for(int j=0;j<BULLET_NUM;j++)
    {
        if(m_duola.m_bullets[j].free)
    {continue;}

        if(m_friends[i].rect[k].intersects(m_duola.m_bullets[j].rect))
        {
            m_friends[i].free[k]=true;
       m_duola.m_bullets[j].free=true;
       for(int m=0;m<money_num;m++)
        {
           if(num_money[m].free)
        {
                num_money[m].free=false;
           num_money[m].x=m_friends[i].x[k];
           num_money[m].y=m_friends[i].y[k];break;}
       }



          for(int m=0;m<money_num;m++)
       {if(num_money[m].free==false)
          {
       if(m_duola.rect.intersects(num_money[m].rect))
       {QSound::play(sound_pick);
           num_money[m].free=true;
       point=point+5;
       if(point==30)
       {m_mood[0].free=false;
               m_mood[0].x=20;
           m_mood[0].y=15;
       }
       if(point==60)
       {m_mood[1].free=false;
               m_mood[1].x=70;
           m_mood[1].y=15;
       }
       if(point==90)
       {m_mood[2].free=false;
               m_mood[2].x=120;
           m_mood[2].y=15;
       }
       if(point==120)
       {m_mood[3].free=false;
               m_mood[3].x=170;
           m_mood[3].y=15;
       }
       if(point==150)
       {m_mood[4].free=false;
               m_mood[4].x=220;
           m_mood[4].y=15;
       }
      if(point>150)
{QSound::play(sound_win);
          m_win.free=false;
      m_win.x=0;m_win.y=0;point=0;break;}
}
       }
}
        }

     }

}





}




